var express = require('express');
var router = express.Router();
var request = require('request');
var md5 = require('md5');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);

// PC端 登录
router.get('/jkyx/pc/login', function(req, res, next) {
	var param = req.query || req.params; 
	// 手机
	var tel = param.tel || 0;
	// 密码
	var passwd = param.passwd || '';
	passwd = md5(passwd);

    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		var lastTimer = CommonJS.getNowFormatDate();
		connection.query(DBConfig.pc_user_login,[lastTimer,tel,passwd,tel,passwd],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var user = results[1];
				if (user.length == 0) {
					res.end(JSON.stringify({
		     			msg: '登录失败',
		     			status:'103'
		     		}));
				}else{
					res.end(JSON.stringify({
		     			msg: '登录成功',
		     			status:'100',
		     			user: user[0]
		     		}));
				}
			}
		})
	})
});

module.exports = router;
